package dj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import dj.action.base.BaseAction;
import dj.model.LoginInfo;
import dj.model.Tuser;
import dj.pageModel.PloginInfo;
import dj.service.LoginInfoService;
import dj.service.UserService;

@Action("loginInfoAction")
public class LoginInfoAction extends BaseAction implements ModelDriven<PloginInfo> {

	private PloginInfo loginInfo = new PloginInfo();

	private LoginInfoService loginInfoService;

	private UserService userService;

	public PloginInfo getModel() {
		return loginInfo;
	}

	public LoginInfoService getLoginInfoService() {
		return loginInfoService;
	}

	@Autowired
	public void setLoginInfoService(LoginInfoService loginInfoService) {
		this.loginInfoService = loginInfoService;
	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void getLoginInfo() {
		Map<String, Object> loginMap = new HashMap<String, Object>();
		try {
			long userNum = loginInfoService.getLoginInfoNum();
			List<LoginInfo> logins = loginInfoService.getLoginInfoPagination();

			List<PloginInfo> login_list = new ArrayList<PloginInfo>();
			if (null != logins && logins.size() > 0) {
				for (LoginInfo login : logins) {
					PloginInfo ploginInfo = new PloginInfo();
					BeanUtils.copyProperties(login, ploginInfo);
					Tuser tuser = userService.getUserById(ploginInfo.getUserid());
					if (null != tuser) {
						ploginInfo.setUserid(tuser.getName());
					}
					login_list.add(ploginInfo);
				}
			}
			loginMap.put("total", userNum);
			loginMap.put("rows", login_list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.writeToJson(loginMap);
	}
}
